package com.design.classroommanagement

import android.view.ViewGroup
import com.alibaba.android.arouter.facade.annotation.Route
import com.cf.annotation.Holder
import com.cf.holder.BaseHolder
import com.cf.holder.QuickAdapter
import com.design.classroommanagement.api.ApiManager
import com.design.classroommanagement.api.Role
import com.design.classroommanagement.api.User
import com.design.classroommanagement.api.getDao
import com.jeremyliao.liveeventbus.LiveEventBus
import com.laughing.lib.base.*
import com.laughing.lib.ui.selector
import com.laughing.lib.utils.*
import kotlinx.android.synthetic.main.item_user.view.*
import kotlinx.coroutines.delay

@Route(path = Pages.TEACHER_LIST)
class TeacherListFragment : HListFragment<TeacherListViewModel>() {

    override fun bindHolder(adapter: QuickAdapter) {
        adapter.bindHolder<UserHolder>()
        binding.root.setBackgroundColor("#eeeeee".parseColor())
    }

    override fun initCompleted() {
        super.initCompleted()
        viewModel.role = arguments<Role>("role").value!!
        when (viewModel.role) {
            Role.admin -> {}
            Role.teacher -> setTitle("老师管理")
            Role.student -> setTitle("学生管理")
        }

        viewModel.updateLiveData.observe(this) {
            onRefresh()
        }
    }

}

class TeacherListViewModel : HBaseListViewModel() {
    var role = Role.teacher
    val updateLiveData = LiveEventBus.get<Boolean>(javaClass.canonicalName!!)
    override fun loadData(result: (List<*>) -> Unit, exception: (Exception) -> Unit) {
        load {
            val userList = ApiManager.getUserList(Role.teacher)
            delay(500)
            userList
        }.result({
            it?.let { it1 -> result(it1) }
        })
    }
}


@Holder
class UserHolder @JvmOverloads constructor(parent: ViewGroup?, layoutId: Int = R.layout.item_user) :
    BaseHolder<User>(parent, layoutId) {
    override fun convert(data: User) {
        itemView.apply {
            nameTv.text = "姓名：" + (data.name ?: "")
            accountTv.text = "账号：" + (data.account ?: "")

            clickWithTrigger {
                openPage(Pages.TeacherPersonalFragment) {
                    withSerializable("user", data)
                }
            }

            setOnLongClickListener {
                topActivity.selector(mutableListOf("删除", "修改", "添加"), "操作") { _, index ->
                    when (index) {
                        0 -> {
                            runInBg {
                                data.role.getDao().delete(data)
                                getVm<TeacherListViewModel>()?.updateLiveData?.post(true)
                            }
                        }
                        1 -> {
                            openPage(Pages.TeacherPersonalFragment) {
                                withSerializable("user", data)
                            }
                        }
                        2 -> {
                            openPage(Pages.TeacherPersonalFragment) {
                                withSerializable("role", data.role)
                            }
                        }
                        else -> {}
                    }
                }
                true
            }
        }
    }
}

